package com.example.day15_7_15_1.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.day15_7_15_1.R;

public class MyProgressView extends View {

    private final float dy;
    private String text;
    private int ringColor;
    private float ringWidth;
    private int circleColor;
    private float startAngle;
    public float sweepAngle;
    private float textSize;
    private int textColor;
    private final Paint arcPaint;
    private final Paint circlePaint;
    private final Paint textPaint;
    private RectF rectF;
    private float cx;
    private float cy;
    private float radio;

    public MyProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgressView);
        if (typedArray != null){
            ringColor = typedArray.getColor(R.styleable.MyProgressView_ring_color, 0);
            ringWidth = typedArray.getDimension(R.styleable.MyProgressView_ring_width, 20);
            circleColor = typedArray.getColor(R.styleable.MyProgressView_circle_color, 0);
            startAngle = typedArray.getFloat(R.styleable.MyProgressView_start_angle, -90);
            sweepAngle = typedArray.getFloat(R.styleable.MyProgressView_sweep_angle, 0);
            textSize = typedArray.getDimension(R.styleable.MyProgressView_android_textSize, 20);
            textColor = typedArray.getColor(R.styleable.MyProgressView_android_textColor, 0);
            text = typedArray.getString(R.styleable.MyProgressView_android_text);
        }
        //环
        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setStrokeWidth(ringWidth);
        arcPaint.setColor(ringColor);
        arcPaint.setStyle(Paint.Style.STROKE);

        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(circleColor);

        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        dy = (fontMetrics.descent - fontMetrics.ascent) * 0.5f - fontMetrics.descent;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int max = Math.max(width, height);
        int spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
        super.onMeasure(spec, spec);
        rectF = new RectF(0.1f * max, 0.1f * max, 0.9f * max, 0.9f * max);
        cx = max/2;
        cy = max/2;
        radio = max/4;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,arcPaint);
        canvas.drawCircle(cx,cy,radio,circlePaint);
        canvas.drawText(text,cx,cy + dy,textPaint);
    }
    public void setProgress(int progress){
        sweepAngle = progress * 3.6f;
        text = "当前下载进度：" + progress + "%";
        postInvalidate();
    }
}
